package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    RoleService roleService;

    @RequestMapping("/add")
    @ExceptionMessage(value = MessageConstant.ADD_ROLE_FALL)
    public Result add(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
            roleService.add(role,menuIds,permissionIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }
    @RequestMapping("/del")
    @ExceptionMessage(value = MessageConstant.DEL_ROLE_FALL)
    public Result del(Integer[] ids){
            roleService.del(ids);
            return new Result(true,MessageConstant.DEL_ROLE_SUCCESS);
    }

    @RequestMapping("/update")
    @ExceptionMessage(value = MessageConstant.EDIT_ROLE_FALL)
    public Result update(@RequestBody Role role,Integer[] menuIds, Integer[] permissionIds){
            roleService.update(role,menuIds,permissionIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/find")
    @ExceptionMessage(value = MessageConstant.GET_ROLE_FALL)
    public Result find(Integer id){
            Role role = roleService.find(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,role);
    }

    @RequestMapping("/findPage")
    @ExceptionMessage(value = MessageConstant.GET_ROLE_FALL)
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult page = roleService.findPage(queryPageBean);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,page);
    }
    @RequestMapping("/findAll")
    @ExceptionMessage(value = MessageConstant.GET_ROLE_FALL)
    public Result findAll(){
        List<Role> roles = roleService.findAll();
        return new Result(true,MessageConstant.GET_ROLE_SUCCESS,roles);
    }
    @RequestMapping("/findRolesIdsByUserId")
    @ExceptionMessage(value = MessageConstant.GET_ROLE_FALL)
    public Result findRolesIdsByUserId(Integer id){
        List<Integer> roleIds = roleService.findRolesIdsByUserId(id);
        return new Result(true,MessageConstant.GET_ROLE_SUCCESS,roleIds);
    }
}
